package com.rem.zookeeper.controller;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author Rem
 * @date 2022-12-10
 */
@RestController
public class CuratorController {

    @Autowired
    CuratorFramework curatorFramework;

    /**
     * 可重入锁
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/lock")
    public String lock() throws Exception {
        InterProcessMutex lock = new InterProcessMutex(curatorFramework, "/lock");
        System.out.println("测试锁重入");
        lock.acquire();
        System.out.println("执行操作中");
        TimeUnit.SECONDS.sleep(2);

        lock.release();
        return "锁已释放";
    }

}
